﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOSE_TDD
{
    public class Article : IEquatable<Article>
    {
        #region fields
        private string articleName = "";
        private string description = "";
        private decimal taxValue = 0.00m;
        private decimal taxRate = 19.00m;
        private decimal netPrice = 0.00m;
        private decimal grossPrice = 0.00m;
        #endregion


        #region properties
        public string ArticleName
        {
            get { return articleName; }
            set { articleName = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public decimal TaxValue
        {
            get { return taxValue; }
            set { taxValue = value; }
        }

        public decimal TaxRate
        {
            get { return taxRate; }
            set { taxRate = value; }
        }

        public decimal NetPrice
        {
            get { return netPrice; }
            set { netPrice = value; }
        }

        public decimal GrossPrice
        {
            get { return grossPrice; }
            set { grossPrice = value; }
        }
        #endregion

        public Article()
        {

        }

        public Article(string articleName, string description,
                            decimal taxRate, decimal grossPrice)
        {
            this.articleName = articleName;
            this.description = description;
            this.taxRate = taxRate;
            this.grossPrice = grossPrice;
        }


        // this method calculates the tax and retuns the value
        public decimal getTax()
        {

            try
            {
                taxValue = (grossPrice / (taxRate + 100) * taxRate);

                if (taxValue < 0)
                {
                    Console.WriteLine("Fehler: Wert darf nicht negativ sein");
                    taxValue = 0;
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Fehler: " + e.Message);
                taxRate = 0;
            }

            return taxValue;


        }

        //this method calculates the netprice and should be called
        public decimal getNetPrice()
        {
            try
            {
                netPrice = grossPrice - getTax();

                if (netPrice < 0)
                {
                    Console.WriteLine("Fehler: Wert darf nicht negativ sein");
                    netPrice = 0;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Fehler: " + e.Message);
                netPrice = 0;
            }

            return netPrice;

        }

        public override bool Equals(Object obj)
        {
            if (obj == null) return base.Equals(obj);

            if (!(obj is Article))
                throw new InvalidCastException("The 'obj' argument is not a Article object.");
            else
                return Equals(obj as Article);
        }

        public bool Equals(Article other)
        {
            if (this.articleName == other.articleName
                && (this.description == other.description)
                && (this.grossPrice == other.grossPrice)
                && (this.taxRate == other.taxRate))
                return true;
            else
                return false;
        }
    }
}
